package morning.cat.bpp;

import morning.cat.service.UserService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

/**
 * @describe: 类描述信息
 * @author: morningcat.zhang
 * @date: 2020/1/9 2:16 PM
 */
@Component
public class UserBeanPostProcessor implements BeanPostProcessor
		, BeanFactoryAware
{

//	@Autowired
	BeanFactory beanFactory;

	@Autowired
	ApplicationContext applicationContext;

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

		if (beanName.equalsIgnoreCase(UserService.class.getSimpleName())) {
			System.out.println("------");
			System.out.println(beanFactory);
			System.out.println(applicationContext);
			System.out.println(applicationContext.getAutowireCapableBeanFactory());

			UserService userService = (UserService) bean;
			userService.setName("默认名字");
			return userService;
		}
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		if (beanName.equalsIgnoreCase(UserService.class.getSimpleName())) {
			UserService userService = (UserService) bean;
			userService.setName("默认名字After");
			return userService;
		}
		return bean;
	}

	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		this.beanFactory=beanFactory;
	}
}
